package com.demo.service.impl;

import com.demo.mapper.ZcMapper;
import com.demo.pojo.Zc;
import com.demo.pojo.Zc;
import com.demo.service.ZcService;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author： Administrator
 * @date: 21/05/13 14:47
 * 描述：
 */
@Service
public class ZcServiceImpl implements ZcService {
    @Autowired
    private ZcMapper zcMapper;

    @Override
    public int add(Zc zc) {
        zc.setZcState("正常");
        return zcMapper.insertSelective(zc);
    }

    @Override
    public int del(String no) {
        return -99;
    }

    @Override
    public int del(Integer id) {
        Zc zc = zcMapper.selectByPrimaryKey(id);
        switch (zc.getZcState()) {
            case "借出":
                return -1;
            case "维修":
                return -2;
            default:
                return zcMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public int update(Zc zc) {
        return zcMapper.updateByPrimaryKeySelective(zc);
    }

    @Override
    public Page<Zc> page(Zc zc, int pageNo, int pageSize) {
        Page<Zc> page = new Page<>();
        page.setPageSize(pageSize);
        int count = zcMapper.pageCount(zc);
        page.setPageTotalCount(count);
        int total = count / pageSize;
        if (count % pageSize > 0) {
            total++;
        }
        page.setPageTotal(total);
        page.setPageNo(pageNo);
        List<Zc> users = zcMapper.pageByCond(zc, (pageNo - 1) * pageSize, pageSize);
        page.setItems(users);
        return page;
    }
}
